package com.yl.routeing.config;

import com.graphhopper.GraphHopper;
import com.graphhopper.config.CHProfile;
import com.graphhopper.config.Profile;
import com.graphhopper.reader.dem.ElevationProvider;
import com.graphhopper.reader.osm.GraphHopperOSM;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.storage.Graph;
import com.graphhopper.storage.GraphHopperStorage;
import com.graphhopper.storage.NodeAccess;
import com.graphhopper.storage.StorableProperties;
import com.graphhopper.storage.index.LocationIndex;
import com.graphhopper.storage.index.LocationIndexTree;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.PostConstruct;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${osm.file.location}")
    private String osmFileLocation;

    @Value("${osm.cache.location}")
    private String osmCacheLocation;

    @PostConstruct
    @Bean
    public GraphHopper InitMapData() {
        long start = System.currentTimeMillis();
        GraphHopper hopper = new GraphHopperOSM().forServer();
        // 扩大精度
        hopper.setPreciseIndexResolution(1000000);
        hopper.setDataReaderFile(osmFileLocation);
        // specify where to store graphhopper files
        hopper.setGraphHopperLocation(osmCacheLocation);
        hopper.setEncodingManager(EncodingManager.create("car"));
        hopper.setProfiles(new Profile("car").setVehicle("car").setWeighting("fastest").setTurnCosts(false));
        hopper.getCHPreparationHandler().setCHProfiles(new CHProfile("car"));
        hopper.getRouterConfig().setCHDisablingAllowed(true);
        // 加载地图路网数据
        hopper.importOrLoad();

        //////////////////////////////////////////////////
        System.out.println("GraphHopperStorage =>");
        GraphHopperStorage ghStorage = hopper.getGraphHopperStorage();
        Graph baseGraph = ghStorage.getBaseGraph();
        long capacity = ghStorage.getCapacity();
        NodeAccess nodeAccess = ghStorage.getNodeAccess();
        int nodes = ghStorage.getNodes();
        StorableProperties properties = ghStorage.getProperties();

        System.out.println("EncodingManager =>");
        EncodingManager encodingManager = hopper.getEncodingManager();


        ElevationProvider elevationProvider = hopper.getElevationProvider();
        //////////////////////////////////////////////////

        // 扩大搜索范围
        LocationIndex locationIndex = hopper.getLocationIndex();
        if (locationIndex instanceof LocationIndexTree) {
            ((LocationIndexTree) locationIndex).setMaxRegionSearch(40);
        }
        long end = System.currentTimeMillis();
        System.out.println("地图路网数据初始化完成！花费时间：" + ((end - start) < 1000 ? 1 : (end - start) / 1000) + "（秒）");

        return hopper;
    }

}
